﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.iss.inventory.Entity;

namespace Stationery_Inventory.Presentation.DeliveryOrder
{
    public partial class CheckDOHistory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Populate To Date Textbox Automatically (Today's Date)
            DateTime dateonly = DateTime.Now.Date;
            tbDateTo.Text = dateonly.ToString("dd-MM-yyyy");
        }

        // Populate the Gridview, with Delivery orders in the range of dates selected
        protected void btnCheckDeliveryOrders_Click(object sender, EventArgs e)
        {
          
            InvManagementEntities invent = new InvManagementEntities();
            DateTime datefrom = Convert.ToDateTime(tbDateFrom.Text);
            DateTime dateto = Convert.ToDateTime(tbDateTo.Text);

            var DOHistory = from d in invent.DeliveryOrders
                            where d.DateDelivered >= datefrom && d.DateDelivered<= dateto

                            select new { d.DeliveryID, d.PurchaseID, d.SignedbyID, d.SupplierBillNo, d.DateDelivered };

            List<object> DOList = new List<object>();
            DOList = DOHistory.ToList<object>();
           gvDeliveryOrdersHistory.DataSource = DOList;
           gvDeliveryOrdersHistory.DataBind();
        }

        //Check what Stationery Items are in each delivery order
        protected void btnCheckDeliveryItems_Click(object sender, EventArgs e)
        {
            
            InvManagementEntities invent = new InvManagementEntities();
          int deliveryID =  Convert.ToInt32(tbDeliveryID.Text);

          var DOItems = from d in invent.DeliveryOrders
                        join p in invent.PurchaseOrders on d.PurchaseID equals p.PurchaseID
                        join podet in invent.PurchaseOrderDetails on p.PurchaseID equals podet.PurchaseID
                        where d.DeliveryID == deliveryID

                        select new {podet.ItemID, podet.Description, 
                        podet.Qty, podet.UnitPrice };

            
          var DOdetails = (from p in invent.PurchaseOrders
                        where p.DeliveryOrderID == deliveryID

                        select p.DeliveryOrderID).First();

         lblDeliveryOrderIDUI.Text = DOdetails.ToString();


            var DOdetails1 = (from p in invent.PurchaseOrders
                             where p.DeliveryOrderID == deliveryID

                             select p.PurchaseID).First();
            lblPurchaseIDUI.Text = DOdetails1.ToString();

            var DOdetails2 = (from p in invent.PurchaseOrders
                              where p.DeliveryOrderID == deliveryID

                              select p.SupplierID).First();

           lblSupplierIDUI.Text= DOdetails2.ToString();


            var DOdetails3 = (from p in invent.PurchaseOrders
                              where p.DeliveryOrderID == deliveryID

                              select p.OrderedbyID).First();

            lblOrderedbyUI.Text = DOdetails3.ToString();

            var DOdetails4 = (from p in invent.PurchaseOrders
                              where p.DeliveryOrderID == deliveryID

                              select p.DateOrdered).First();
            lblDateOrderedUI.Text = DOdetails4.ToString();

            var DOdetails5 = (from p in invent.PurchaseOrders
                              where p.DeliveryOrderID == deliveryID

                              select p.DateRequired).First();
            lblDateRequiredUI.Text = DOdetails5.ToString();

            var DOdetails6 = (from p in invent.PurchaseOrders
                              where p.DeliveryOrderID == deliveryID

                              select p.Remarks).First();
            tbRemarks.Text = DOdetails6.ToString();

                       // , p.PurchaseID, p.SupplierID, p.OrderedbyID, 
                      //  p.DateOrdered, p.DateRequired, p.Remarks}; 
            
      // tbRemarks.Text = Convert.ToString(DOdetails(6));
          
            
        //   Array DODetailsArray = DOdetails.ToArray();
          //  DODetailsArray.
        

          List<object> DOItemsList = new List<object>();
            
          DOItemsList = DOItems.ToList<object>();
          gvDeliveryRecordItems.DataSource = DOItemsList;
          gvDeliveryRecordItems.DataBind();
        }
    }
}